package Book;

import java.util.Scanner;

public class BookManager {
    static Scanner sc = new Scanner(System.in);
    static String[][] users = new String[20][4];
    static String[][] books = new String[20][5];
    static String[][] pubs = new String[20][3];
    static String loginUser,identity;
     //主程序
     public static void main(String[] args) {
         administrators();
         books();
         pubs();
         index();
     }
    //登陆系统
    private static void index(){
        while(true){
            System.out.println("欢迎来到龙院图书管理系统！");
            System.out.println("1.登录 2.注册");
            int logOrReg = sc.nextInt();
            switch (logOrReg){
                case 1:
                    login();
                    break;
                case 2:
                    register();
                    break;
                default:
                    System.out.println("系统退出！");
                    System.exit(0);
            }
        }
    }

    //用户注册
    private static void register(){
        System.out.println("请输入所属部门：");
        String deptName = sc.next();
        System.out.println("请输入用户名：");
        String userName = sc.next();
        System.out.println("请输入密码：");
        String passWord = sc.next();
        for (int i = 0;i <= users.length;i++){
            if(users[i][0] == null){
                users[i][0] = deptName;
                users[i][1] = userName;
                users[i][2] = passWord;
                users[i][3] = "普通用户";
                System.out.println("注册成功！");
                break;
            }
        }
    }

    //用户登录
    private static void login(){
        while(true){
            String userName;
            String passWord;
            System.out.println("请输入用户名：");
            userName = sc.next();
            System.out.println("请输入密码：");
            passWord = sc.next();
            if(userName == null || passWord == null){
                System.out.println("用户名或密码不能为空！请重新输入！");
            }else{
                for (int i = 0;i < users.length;i++){
                    if (userName.equals(users[i][1]) && passWord.equals(users[i][2])){
                        System.out.println("登陆成功！");
                        loginUser = userName;
                        identity = users[i][3];
                        homePage();
                    }
                }
                System.out.println("用户名或密码错误！请重新输入！");
            }
        }
    }

    //图书管理系统
    private static void homePage(){
        System.out.println(loginUser + ",欢迎您使用龙院书籍管理系统！！！");
        while(true){
            System.out.println("请输入数字进行选择：1 图书管理  2 出版社管理  3 退出登录  4 退出系统");
            int logOrReg = sc.nextInt();
            switch (logOrReg){
                case 1:
                    bookManage();
                    break;
                case 2:
                    pubManage();
                    break;
                case 3:
                    loginUser = "";
                    System.out.println("已退出登录！");
                    index();
                    break;
                case 4:
                    System.out.println("系统退出成功！");
                    System.exit(0);
            }
        }
    }

    //图书管理
    private static void bookManage(){
        while(true){
            System.out.println("请输入：1.增加 2.删除 3.更新 4.查询 5.返回上一级菜单");
            int key = sc.nextInt();
            switch(key){
                case 1:
                    addBook();
                    break;
                case 2:
                    delBook();
                    break;
                case 3:
                    updateBook();
                    break;
                case 4:
                    searchBook();
                    break;
                case 5:
                    homePage();
            }
        }
    }

    //增加图书
    private static void addBook(){
        String isbn,book_name,price,pub,author;
        System.out.println("请输入图书ISBN:");
        isbn = sc.next();
        System.out.println("请输入书名:");
        book_name = sc.next();
        System.out.println("请输入价格:");
        price = sc.next();
        System.out.println("请输入出版社:");
        pub = sc.next();
        System.out.println("请输入作者:");
        author = sc.next();
        for(int i = 0;i <= books.length;i++){
            if(books[i][0] == null){
                books[i][0] = isbn;
                books[i][1] = book_name;
                books[i][2] = price;
                books[i][3] = pub;
                books[i][4] = author;
                break;
            }
        }
        System.out.println("添加成功！！！");
        bookManage();
    }

    //删除图书
    private static void delBook(){
        if(!identity.equals("管理员")){
            System.out.println("你不是管理员，无权删除。");
            bookManage();
        }else{
            System.out.println("请输入要删除的书本名称：");
            String book_name = sc.next();
            for(int i = 0; i < books.length;i++){
                if(book_name.equals(books[i][1])){
                    books[i][0] = null;
                    books[i][1] = null;
                    books[i][2] = null;
                    books[i][3] = null;
                    books[i][4] = null;
                    System.out.println("删除成功！");
                    break;
                }
                bookManage();
            }
        }
    }

    //更新图书
    private static void updateBook(){
        if(!identity.equals("管理员")){
            System.out.println("你不是管理员，无权更改。");
            bookManage();
        }else{
            System.out.println("请输入ISBN号：");
            String isbn = sc.next();
            for(int i = 0;i < books.length;i++){
                if(isbn.equals(books[i][0])){
                    String book_name,price,pub,author;
                    System.out.println("请输入书名:");
                    book_name = sc.next();
                    System.out.println("请输入价格:");
                    price = sc.next();
                    System.out.println("请输入出版社:");
                    pub = sc.next();
                    System.out.println("请输入作者:");
                    author = sc.next();
                    books[i][0] = isbn;
                    books[i][1] = book_name;
                    books[i][2] = price;
                    books[i][3] = pub;
                    books[i][4] = author;
                    System.out.println("更新成功！");
                    bookManage();
                    break;
                }
            }
            System.out.println("该ISBN号不存在！！！");
            bookManage();
        }
    }

    //查询图书
    private static void searchBook(){
        while(true){
            System.out.println("请输入查询种类：1.isbn 2.书名（模糊） 3.出版社 4. 作者 5. 价格范围 6.查询所有 7.返回上一级");
            int h = sc.nextInt();
            switch(h){
                case 1:
                    searchIsbn();
                    break;
                case 2:
                    searchBookName();
                    break;
                case 3:
                    searchPub();
                    break;
                case 4:
                    searchAuthor();
                    break;
                case 5:
                    searchPrice();
                    break;
                case 6:
                    searchAllBook();
                    break;
                case 7:
                    bookManage();
            }
         }
    }

    //查询图书ISBN号
    private static void searchIsbn(){
        System.out.println("请输入ISBN号：");
        String isbn = sc.next();
        System.out.println("\tISBN\t    书名     \t价格      \t出版社          \t作者\t");
        for(int i = 0; i < books.length;i++){
            if(isbn.equals(books[i][0])){
                System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",books[i][0],books[i][1],books[i][2],books[i][3],books[i][4]);
                break;
            }
        }
        searchBook();
    }

    //查询书名
    private static void searchBookName(){
        System.out.println("请输入书名");
        String book_name = sc.next();
        System.out.println("\tISBN\t    书名     \t价格      \t出版社          \t作者\t");
        for(int i = 0; i < books.length;i++){
            if(book_name.equals(books[i][1])){
                System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",books[i][0],books[i][1],books[i][2],books[i][3],books[i][4]);
                break;
            }
        }
        searchBook();
    }

    //查询出版社
    private static void searchPub(){
        System.out.println("请输入出版社:");
        String pub = sc.next();
        System.out.println("\tISBN\t    书名     \t价格      \t出版社          \t作者\t");
        for(int i = 0; i < books.length; i++){
            if(pub.equals(books[i][3])){
                System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",books[i][0],books[i][1],books[i][2],books[i][3],books[i][4]);
            }
        }
        searchBook();
    }

    //查询作者
    private static void searchAuthor(){
        System.out.println("请输入作者名字：");
        String author = sc.next();
        System.out.println("\tISBN\t    书名     \t价格      \t出版社          \t作者\t");
        for(int i = 0; i < books.length;i++){
            if(author.equals(books[i][4])){
                System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",books[i][0],books[i][1],books[i][2],books[i][3],books[i][4]);
            }
        }
        searchBook();
    }

    //查询价格范围
    private static void searchPrice(){
        double  lowest_price;
        double highest_price;
        System.out.println("请输入最低价格：");
        lowest_price = sc.nextDouble();
        System.out.println("请输入最高价格：");
        highest_price = sc.nextDouble();
        System.out.println("\tISBN\t    书名     \t价格      \t出版社          \t作者\t");
        for(int i = 0;i < books.length;i++){
            if(books[i][0] != null){
                double price=Double.parseDouble(books[i][2]);
                if(lowest_price <= price && highest_price >= price && books[i][2] != null){
                    System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",books[i][0],books[i][1],books[i][2],books[i][3],books[i][4]);
                }
            }
        }
        searchBook();
    }

    //查询所有图书
    private static void searchAllBook(){
        System.out.println("\tISBN\t    书名     \t价格      \t出版社          \t作者\t");
        for(int i = 0;i < books.length;i ++){
            if (books[i][0] != null ) {
                System.out.printf("%-12s\t%-7s\t%-10s\t%-10s\t%-10s\n",books[i][0],books[i][1],books[i][2],books[i][3],books[i][4]);
            }
        }
    }
    //出版社管理
    private static void pubManage(){
        System.out.println("请输入：1.增加 2.删除 3.更新 4.根据出版社名称查询 5.查询所有出版社 6.返回上一级菜单");
        int key = sc.nextInt();
        switch (key){
            case 1:
                addPub();
                break;
            case 2:
                delPub();
                break;
            case 3:
                updatePub();
                break;
            case 4:
                searchByPub();
                break;
            case 5:
                searchAllPub();
                break;
            case 6:
                homePage();
        }
    }

    //添加出版社
    private static void addPub(){
        while(true){
            System.out.println("请输入出版社名称：");
            String pname = sc.next();
            for(int i = 0;i < pubs.length;i ++){
                if(pname.equals(pubs[i][0])){
                    System.out.println("该出版社已存在！请重新输入！");
                    break;
                }else{
                    if(pubs[i][0] == null){
                        System.out.println("请输入出版社地址：");
                        String padress = sc.next();
                        System.out.println("请输入出版社联系人：");
                        String pcontacts = sc.next();
                        pubs[i][0] = pname;
                        pubs[i][1] = padress;
                        pubs[i][2] = pcontacts;
                        System.out.println("出版社添加成功");
                        pubManage();
                        break;
                    }
                }
            }
        }
    }

    //删除出版社
    private static void delPub(){
        if(!identity.equals("管理员")) {
            System.out.println("你不是管理员，无权更改。");
            bookManage();
        }else{
            System.out.println("请输入要删除的出版社：");
            String pub = sc.next();
            for(int i = 0; i < books.length;i++){
                if(pub.equals(books[i][3])){
                    System.out.println("书库中有该出版社的书籍！");
                    System.out.println("删除失败！");
                }else{
                    for(int j = 0;j < pubs.length;j++){
                        if(pub.equals(pubs[j][0])){
                            pubs[j][0] = null;
                            pubs[j][1] = null;
                            pubs[j][2] = null;
                            System.out.println("删除成功！");
                            pubManage();
                            break;
                        }
                    }
                    System.out.println("不存在该出版社！删除失败！");
                }
                pubManage();
                break;
            }
        }
    }

    //更新出版社
    private static void updatePub(){
        if(!identity.equals("管理员")) {
            System.out.println("你不是管理员，无权更改。");
            bookManage();
        }else{
            System.out.println("请输入要更新的的出版社：");
            String pub = sc.next();
            for(int i = 0; i < books.length;i++){
                if(pub.equals(books[i][3])){
                    System.out.println("请输入新地址：");
                    String padress = sc.next();
                    System.out.println("请输入新联系人：");
                    String pcontacts = sc.next();
                    pubs[i][1] = padress;
                    pubs[i][2] = pcontacts;
                    System.out.println("更新成功！");
                    pubManage();
                    break;
                }
                System.out.println("更新失败！不存在该出版社！");
                pubManage();
                break;
            }
        }
    }

    //根据出版社名称查询
    private static void searchByPub(){
        System.out.println("请输入要查询的的的出版社：");
        String pub = sc.next();
        System.out.println("出版社名称\t    地址                      \t联系人");
        for(int i = 0;i < pubs.length;i++){
            if(pub.equals(pubs[i][0])){
                System.out.printf("%-10s\t%-20s\t%-4s\n",pubs[i][0],pubs[i][1],pubs[i][2]);
            }
            pubManage();
            break;
        }
    }
    //查询所有出版社
    private static void searchAllPub(){
        System.out.println("出版社名称\t    地址                      \t联系人");
         for(int i = 0;i < pubs.length; i++){
             if(pubs[i][0] != null){
                 System.out.printf("%-10s\t%-20s\t%-4s\n",pubs[i][0],pubs[i][1],pubs[i][2]);
             }
         }
         pubManage();
    }

    //图书管理系统管理员
    private static void administrators(){

        users[0][0] = "行政部";
        users[0][1] = "aaa";
        users[0][2] = "123456";
        users[0][3] = "管理员";

        users[1][0] = "市场部";
        users[1][1] = "张三";
        users[1][2] = "123";
        users[1][3] = "普通用户";
    }

    //图书
    private static void books(){
        books[0][0] = "97871010";
        books[0][1] = "史记";
        books[0][2] = "125.0";
        books[0][3] = "中华书局";
        books[0][4] = "司马迁";

        books[1][0] = "97875063";
        books[1][1] = "活着";
        books[1][2] = "20.0";
        books[1][3] = "作家出版社";
        books[1][4] = "余华";

        books[2][0] = "97872290";
        books[2][1] = "三体全集";
        books[2][2] = "168.0";
        books[2][3] = "重庆出版社";
        books[2][4] = "刘慈欣";

        books[3][0] = "97875442";
        books[3][1] = "白夜行";
        books[3][2] = "39.5";
        books[3][3] = "南海出版公司";
        books[3][4] = "[日]东野圭吾";
    }

    //出版社
    private static void pubs(){
        pubs[0][0] = "中华书局";
        pubs[0][1] = "北京市王府井大街36号";
        pubs[0][2] = "张三";

        pubs[1][0] = "作家出版社";
        pubs[1][1] = "香港九龙荷李活商业中心8楼";
        pubs[1][2] = "李四";

        pubs[2][0] = "重庆出版社";
        pubs[2][1] = "茶园新区开拓路6号";
        pubs[2][2] = "王五";

        pubs[3][0] = "南海出版公司";
        pubs[3][1] = "海秀中路51-1号星华大厦5楼";
        pubs[3][2] = "周毅";
    }
}
